package edu.maping.easy.painter;

import controlP5.ControlEvent;
import controlP5.ControlGroup;
import processing.core.PApplet;
import processing.core.PGraphics;
import edu.maping.easy.ControlFacade;
import edu.maping.easy.gui.ButtonList;
import edu.maping.easy.gui.GUIEvent;
import edu.maping.easy.gui.GuiFactory;
import edu.maping.easy.model.PhysicPolyThing;
import edu.maping.easy.model.Point;
import edu.maping.easy.model.Thing;
import edu.maping.easy.sequence.ActionConf;

public class ThingPainterTest implements ThingPainterDelegate {

	PGraphics pg;

	int pixelSize = 2;

	public void draw(Thing cosa, PApplet pApplet, Object object) {

		pApplet.pushStyle();

		PhysicPolyThing poly = (PhysicPolyThing) cosa;
		if (pg == null) {
			pg = pApplet.createGraphics(160, 90, PApplet.P2D);
			pApplet.colorMode(PApplet.HSB);
			pApplet.noSmooth();
		}

		int xx = 0;
		int yy = 0;
		int sum = 0;

		float xc = 25;

		// Enable this to control the speed of animation regardless of CPU power
		// int timeDisplacement = millis()/30;

		// This runs plasma as fast as your computer can handle
		int timeDisplacement = pApplet.frameCount;

		// No need to do this math for every pixel
		float calculation1 = PApplet.sin(PApplet
				.radians((float) (timeDisplacement * 0.61655617)));
		float calculation2 = PApplet.sin(PApplet
				.radians((float) (timeDisplacement * -3.6352262)));

		// Output into a buffered image for reuse
		pg.beginDraw();
		pg.loadPixels();

		// Plasma algorithm
		for (int x = 0; x < pg.width; x++, xc += pixelSize) {
			float yc = 25;
			float s1 = 128 + 128 * PApplet.sin(PApplet.radians(xc)
					* calculation1);

			for (int y = 0; y < pg.height; y++, yc += pixelSize) {
				float s2 = 128 + 128 * PApplet.sin(PApplet.radians(yc)
						* calculation2);
				float s3 = 128 + 128 * PApplet.sin(PApplet
						.radians((xc + yc + timeDisplacement * 5) / 2));
				float s = (s1 + s2 + s3) / 3;
				pg.pixels[x + y * pg.width] = pApplet.color((int) s,
						(int) (255 - s / 2.0), 255);
			}
		}
		pg.updatePixels();
		pg.endDraw();

		// display the results

		pApplet.beginShape(PApplet.QUAD_STRIP);
		for (int i = 0; i < poly.getVertex().size(); i++) {
			Point point = poly.getVertex().get(i);
			pApplet.vertex(point.x, point.y);
		}

		pApplet.texture(pg);

		pApplet.endShape();

		pApplet.popStyle();
	}

	public void draw(Thing body, PApplet pApplet) {
		draw(body, pApplet, null);
	}

	@Override
	public void setConf(ActionConf actionConf) {
		// TODO Auto-generated method stub

	}

	@Override
	public String getName() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public EventPainter controlEvent(GUIEvent theEvent) {
		// TODO Auto-generated method stub
		return null;
	}

	public void setup(PApplet pApplet, ControlFacade controlFacade,
			ButtonList buttonList, GuiFactory guiFactory) {
	}

	@Override
	public void showForm(int x, int y) {
		// TODO Auto-generated method stub

	}

	@Override
	public void hideForm() {
		// TODO Auto-generated method stub

	}

	@Override
	public ActionConf createNewActionConf() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ThingPainterDelegate createNewInstance() {
		// TODO Auto-generated method stub
		return null;
	}

}
